using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class PX_CourseClassService : BaseAppService<PX_CourseClassDto, PX_CourseClass, PX_CourseClassRepository>
    {
        readonly ExcelHelp _excelHelp;
        public PX_CourseClassService(PX_CourseClassRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        private LoadPageOffsetModelAuto<PX_CourseClass, PX_CourseClassDto>
            GetLoadPageOffsetMode(PagingModel<PX_CourseClassDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("Name", "=")
     .Add("PId", "=")
     .Add("Remark", "=")
     .Add("IsDel", "=")
     .Add("CreateFactory", "=")
     .Add("CreateDept", "=")
     .Add("UpdateDept", "=");


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new PX_CourseClass()
            {
                Id = model.Id,
                Name = model.Name,
                PId = model.PId,
                Remark = model.Remark,
                IsDel = model.IsDel,
                CreateFactory = model.CreateFactory,
                CreateDept = model.CreateDept,
                UpdateDept = model.UpdateDept,
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<PX_CourseClassDto>> GetList(PagingModel<PX_CourseClassDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            this.GetLoadPageOffsetModel(pagingQueryModel);

            //基础查询，联表查询在此操作
            var baseQuery = _repository.QueryBase().AsNoTracking();

            //查询条件
            loadPageOffsetModel.QueryWhere = this.CreateQRList().Add("CreateFactory","=");//.Add(nameof(SysDepartmentDto.Name), "in");//.Add(nameof(SysDepartmentDto.ParentId), "=").Add(nameof(SysDepartmentDto.CreateFactory), "=");
            //loadPageOffsetModel.QueryWhere = this.CreateQRList();

            loadPageOffsetModel.BasqQuery = baseQuery;

            //指定查询字段 
            loadPageOffsetModel.Selector = u => new PX_CourseClass()
            {
                Id = u.Id,
                PId = u.PId,
                Name = u.Name

            };

            //Task<IPageModel<SysDepartmentDto>> res =  base.LoadPageOffsetAsync(loadPageOffsetModel);
            //return null;
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        /// <summary>
        /// 逻辑删除，继承自基础服务类物理删除方法并重写调用基础仓储类逻辑删除方法
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="autoSave"></param>
        /// <returns></returns>
        public override async Task DeleteBatchAsync(Guid[] ids, bool autoSave = true)
        {
            foreach (Guid item in ids)
            {
                //调用基础仓储类中的逻辑删除方法
                await _repository.DeleteLogicAsync(item);
            }
        }

        public async Task<string>
            Export(PagingModel<PX_CourseClassDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.Name,
                Name_title = "分类名称",
                model.PId,
                PId_title = "上级分类ID",
                model.Remark,
                Remark_title = "备注",
                model.IsDel,
                IsDel_title = "删除标志",
                model.CreateFactory,
                CreateFactory_title = "隶属工厂",
                model.CreateDept,
                CreateDept_title = "添加部门",
                model.UpdateDept,
                UpdateDept_title = "更改部门",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}
